
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class VipHouseBOList : Csla.BusinessListBase<VipHouseBOList, VipHouseBO>
	{
        #region ham lay tat ca danh sach tin vip
        public static VipHouseBOList GetAllVipHousesBOList()
        {
            return DataPortal.Fetch<VipHouseBOList>();
        }
        #endregion
		#region Authorization Rules

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of VipHouseBOList
			//AuthorizationRules.AllowCreate(typeof(VipHouseBOList), "VipHouseBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(VipHouseBOList), "VipHouseBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(VipHouseBOList), "VipHouseBOListDeleteGroup"));
		}
		#endregion //Authorization Rules
        #region truy cap du lieu cua ham lay tat ca danh sach tin vip
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch()
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;

                this.AddRange(
                    from row in mgr.DataContext.VipHouses
                    select VipHouseBO.GetVipHouseBO(row.VipHouseID)
                );
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion

        #region Ham lay tin ve tu he thong SMS de cap nhat danh sach tin vip
        #region ham
        public static void GetNewsHouse_FromSMS(DateTime thoigian_hengio)
        {         
            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                var result = from row in mgr.DataContext.SMSDatas
                             select row;
                int total = result.Count();
                int countNewsHouse = 0, countVipHouse = 0;
                bool check = false;
                VipHouseBO vipHouseBO;
                NewsHouseBO newshouse;
                foreach (var detail in result)
                {
                    countNewsHouse = 0;
                    countVipHouse = 0;
                    countNewsHouse = mgr.DataContext.NewsHouses.Count(n => n.NewsHouseID == detail.NewsHouseID);
                    if (countNewsHouse > 0)
                    {
                        countVipHouse = mgr.DataContext.VipHouses.Count(m => m.NewsHouseID == detail.NewsHouseID);
                        if (countVipHouse > 0)
                        {
                            var viphouselist = VipHouseBOList.GetVipHouseBOList(detail.NewsHouseID);
                            check = false;
                            foreach (var listdetail in viphouselist)
                            {
                                //neu co tin bang gia thi kiem tra neu chua het han thi tang so lan - neu het han thi xoa va luu lai tin moi
                                if (detail.Price == listdetail.Price)
                                {
                                    //neu co tin bang gia thi gan check = true
                                    check = true;
                                    //kiem tra neu tin vip neu het han thi dang ky lai tin moi nguoc lai chua het han thi tang so lan dang ky tin vip len
                                    if ((Convert.ToDateTime(listdetail.RegisterDate).AddDays(double.Parse((listdetail.NumPost * listdetail.NumRegister).ToString()))) < DateTime.Now)
                                    {
                                        //truong hop tin vip da het han
                                        vipHouseBO = VipHouseBO.GetVipHouseBO(listdetail.VipHouseID);
                                        if (vipHouseBO.Price == PriceAdvertisementBO.checkvip_newshouse("max"))
                                            vipHouseBO.NumPost = 5;
                                        else vipHouseBO.NumPost = 3;
                                        vipHouseBO.NumRegister = 1;
                                        vipHouseBO.IsActive = true;
                                        vipHouseBO.IsExtension = false;
                                        vipHouseBO.RegisterDate = DateTime.Now;
                                        //cap nhat ngay het han cua tin
                                        newshouse = NewsHouseBO.GetNewsHouseBO(detail.NewsHouseID);
                                        if(newshouse.ExpireDate < DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString())))
                                            newshouse.ExpireDate = DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString()));
                                        try
                                        {
                                            vipHouseBO.Save();
                                            newshouse.Save();
                                        }
                                        catch { }
                                    }
                                    else
                                    {
                                        //truong hop tin vip van con han dang thi tang so lan dang ky len
                                        vipHouseBO = VipHouseBO.GetVipHouseBO(listdetail.VipHouseID);
                                        vipHouseBO.NumRegister += 1;
                                        vipHouseBO.IsActive = true;
                                        newshouse = NewsHouseBO.GetNewsHouseBO(detail.NewsHouseID);
                                        if (newshouse.ExpireDate < DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString())))
                                            newshouse.ExpireDate = DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString()));
                                        try
                                        {
                                            vipHouseBO.Save();
                                            newshouse.Save();
                                        }
                                        catch { }
                                    }
                                }
                            }
                            //neu chech  = false thi ko tim thay tin bang gia => cap nhat thoi gian het han, hoac gia cho tin do
                            if (check == false)
                            {
                                //vipHouseBO = VipHouseBO.GetVipHouseBO_ByIDNewsHouses(detail.NewsHouseID);
                                //if (detail.Price == PriceAdvertisementBO.checkvip_newshouse("max"))
                                //{

                                //}
                                //else
                                //{

                                //}

                                vipHouseBO = VipHouseBO.NewVipHouseBO();
                                vipHouseBO.NewsHouseID = detail.NewsHouseID;
                                vipHouseBO.RegisterDate = DateTime.Now;
                                vipHouseBO.NumRegister = 1;
                                vipHouseBO.IsActive = true;
                                vipHouseBO.IsExtension = false;
                                vipHouseBO.Price = detail.Price;
                                if (detail.Price == PriceAdvertisementBO.checkvip_newshouse("max"))
                                    vipHouseBO.NumPost = 5;
                                else vipHouseBO.NumPost = 3;
                                newshouse = NewsHouseBO.GetNewsHouseBO(detail.NewsHouseID);
                                if (newshouse.ExpireDate < DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString())))
                                    newshouse.ExpireDate = DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString()));
                                try
                                {
                                    vipHouseBO.Save();
                                    newshouse.Save();
                                }
                                catch { }
                            }                            
                        }
                        else
                        {
                            //them moi tin vi trong csdl chua co tin vip nay
                            vipHouseBO = VipHouseBO.NewVipHouseBO();
                            vipHouseBO.NewsHouseID = detail.NewsHouseID;
                            vipHouseBO.RegisterDate = DateTime.Now;
                            vipHouseBO.NumRegister = 1;
                            vipHouseBO.IsActive = true;
                            vipHouseBO.Price = detail.Price;
                            vipHouseBO.IsExtension = false;
                            if (detail.Price == PriceAdvertisementBO.checkvip_newshouse("max"))
                                vipHouseBO.NumPost = 5;
                            else vipHouseBO.NumPost = 3;
                            newshouse = NewsHouseBO.GetNewsHouseBO(detail.NewsHouseID);
                            if (newshouse.ExpireDate < DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString())))
                                newshouse.ExpireDate = DateTime.Now.AddDays(double.Parse((vipHouseBO.NumPost * vipHouseBO.NumRegister).ToString()));
                            try
                            {
                                vipHouseBO.Save();
                                newshouse.Save();
                            }
                            catch { }
                        }
                    }
                }
                //khi lay het tin ve thi xoa cac tin da lay tren csdl SMS
                int i = 0;
                foreach (var detaildelete in result)
                {
                    if (i == total)
                        break;
                    try
                    {
                        SMSDataBO.DeleteSMSDataBO(detaildelete.SMSID);
                        i += 1;
                    }
                    catch { }
                }
            }//using
        }
        #endregion
        #endregion

        #region Ham kiem tra tin vip ton tai - con han dang - Gia = max => Dang o vi tri quang cao tren
        public static bool KiemtraTinVip_TonTai_ConHanDang_ViTriTren(int Newshouseid)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                var result = from row in mgr.DataContext.VipHouses
                             where row.NewsHouseID == Newshouseid && row.Price == PriceAdvertisementBO.checkvip_newshouse("max")
                             select row;
                count = result.Count();            
                if (count > 0)
                {
                    VipHouseBO vipbo = VipHouseBO.GetVipHouseBO_ByIDNewsHouses(Newshouseid);
                    if ((Convert.ToDateTime(vipbo.RegisterDate).AddDays(double.Parse((vipbo.NumPost * vipbo.NumRegister).ToString()))) < DateTime.Now)
                    {
                        return false;
                    }
                    else return true;

                }
                else return false;
            }
        }

        #region Ham lay 1 tin vip quang cao vi tri tren - co dang ky - con han dang

        #endregion
        #endregion
    }
}
